const {Tray, ipcMain} = require('electron');

let tray = null;
const ObjectElement = ['title','content'];
function notification(){

	if(tray == null){
		_initTray();
	}

	function _initTray(){
		tray = _getTray();

		ipcMain.on('notifyMainProcess',function(events, argument){
			if(_validateArgument(argument)){
				tray.displayBalloon(argument);
			}
			//log
  		});
	}

	function _getTray(){
		if(tray == null){
			tray = new Tray(__dirname + '/../app-icon.ico');
		}
		return tray;
	}

	function _validateArgument(argument){
		if(argument == null){
			return false;
		}

		for(var element in argument){
			if(_elNoExistInObjectElement(element, argument)){
				return false;
			}
		}

		return true;
	}

	function _elNoExistInObjectElement(el, argument){
		if(typeof el === 'function'){
			return false;
		}
		if(argument.hasOwnProperty(el) && ObjectElement.indexOf(argument[el])== -1){
			return false;
		}
		return true;
	}
}

exports.notification  = notification;


